<template>
  <el-dialog :modal-append-to-body="false" @close="fpzjyCancel('fpzjyRuleForm')" :close-on-click-modal="false"
    title="分配造价员" :visible.sync="fpzjyVisible" width="500px">
    <el-form :model="fpzjyRuleForm" :rules="fpzjyRules" ref="fpzjyRuleForm" label-position="top">
      <el-form-item label="造价员" prop="cost_estimator_id">
        <el-select class="input-box" v-model="fpzjyRuleForm.cost_estimator_id" placeholder="请选择" filterable clearable>
          <el-option v-for="item in costEstimatorList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input :rows="4" class="input-box" v-model="fpzjyRuleForm.remark" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="fpzjyCancel('fpzjyRuleForm')">关闭</el-button>
      <el-button type="primary" @click="fpzjySubmit('fpzjyRuleForm')" :disabled="diaLoading" :loading="diaLoading">确
        定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import projectApi from "@/api/projectApi";
import companyApi from "@/api/companyApi";
import adminApi from "@/api/adminApi";
import { getUserInfo } from "@/utils/auth";
import foremanApi from "@/api/foremanApi";

export default {
  name: "",
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false,
      required: false,
    },
    project_id: {
      required: false,
    },
  },
  data() {
    return {
      fpzjyVisible: false,
      fpzjyRuleForm: {
        cost_estimator_id: "",
        remark: "",
      },
      fpzjyRules: {
        cost_estimator_id: [
          { required: true, message: "请选择造价员", trigger: "change" },
        ],
      },
      costEstimatorList: [], //造价员
      diaLoading: false,
    };
  },
  watch: {
    addDialogVisible(val1, val2) {
      if (this.addDialogVisible) {
        this.getEngineering_cost_estimator();
      }
      this.fpzjyVisible = this.addDialogVisible;
    },
  },
  computed: {},
  mounted() { },
  methods: {
    // 分配造价员
    fpzjyCancel(formName) {
      this.fpzjyRuleForm = {
        cost_estimator_id: "",
        remark: "",
      };
      this.$nextTick(() => {
        this.$refs[formName].clearValidate();
        this.$emit("onClose", true);
      });
    },
    fpzjySubmit(formName) {
      let fpzjyRuleForm = this.fpzjyRuleForm;
      console.log(fpzjyRuleForm, "分配造价员参数");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.diaLoading = true;
          projectApi
            .assignCostEstimator({
              project_id: this.project_id,
              cost_estimator_id: fpzjyRuleForm.cost_estimator_id,
              remark: fpzjyRuleForm.remark,
            })
            .then((res) => {
              console.log(res, "分配造价员");
              this.diaLoading = false;
              if (res.code == 200) {
                this.$emit("onSuccess", true);
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getEngineering_cost_estimator() {
      var obj = {
        page: 1,
        num: 9999,
        keyword: "",
        province_id: "",
        city_id: "",
        county_id: "",
        identity: 1,
        position_id_arr: [5],
      };
      adminApi
        .getAdminList(obj)
        .then((res) => {
          console.log(res, "造价员");
          if (res.code == 200) {
            this.costEstimatorList = res.data.list;
          }
        })
        .catch((err) => { });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>